package cn.cqautotest.cqviehelper.presenter;

import android.util.ArrayMap;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.haibin.calendarview.Calendar;
import com.vondear.rxtool.RxTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.cqautotest.cqviehelper.beans.AppData;
import cn.cqautotest.cqviehelper.interfaces.IAppData;
import cn.cqautotest.cqviehelper.interfaces.IAppDataCallback;
import cn.cqautotest.cqviehelper.utils.LogUtils;
import cn.cqautotest.cqviehelper.utils.UpdateAppDataUtils;
import cn.cqautotest.cqviehelper.utils.network.MyOkHttp;
import cn.cqautotest.cqviehelper.utils.network.model.HttpData;

public class UpdateAppDataPresenter implements IAppData {

    private static volatile UpdateAppDataPresenter singleton = null;
    private List<IAppDataCallback> mCallbacks = new ArrayList<>();

    private UpdateAppDataPresenter() {

    }

    public static UpdateAppDataPresenter getInstance() {
        if (singleton == null) {
            synchronized (UpdateAppDataPresenter.class) {
                if (singleton == null) {
                    singleton = new UpdateAppDataPresenter();
                }
            }
        }
        return singleton;
    }

    @Override
    public void updateSidebarTime() {
        RxTool.getBackgroundHandler().post(() -> {
            try {
                AppData appData = UpdateAppDataUtils.getAppData();
                String sidebarTimeUrl = appData.getSidebarTime();
                String sidebarTime = MyOkHttp.postData(sidebarTimeUrl, null, null);
                HttpData<String[]> httpDataTimes = GsonUtils.fromJson(sidebarTime, new TypeToken<HttpData<String[]>>() {
                }.getType());
                String[] times = httpDataTimes.getData();
                ThreadUtils.getMainHandler().post(() -> {
                    for (IAppDataCallback callback : mCallbacks) {
                        callback.onSidebarTimeUpdate(times);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ThreadUtils.getMainHandler().post(() -> ToastUtils.showShort("侧边栏时间同步失败！"));
            }
        });
    }

    @Override
    public void updateSchoolCalendar() {
        RxTool.getBackgroundHandler().post(() -> {
            try {
                AppData appData = UpdateAppDataUtils.getAppData();
                String schoolCalendarUrl = appData.getSchoolCalendar();
                String schoolCalendar = MyOkHttp.postData(schoolCalendarUrl, null, null);
                HttpData<List<Calendar>> schoolCalendarHttpData = GsonUtils.fromJson(schoolCalendar, new TypeToken<HttpData<List<Calendar>>>() {
                }.getType());
                List<Calendar> calendars = schoolCalendarHttpData.getData();
                Map<String, Calendar> schoolCalendarMap = new ArrayMap<>();
                for (Calendar calendar : calendars) {
                    schoolCalendarMap.put(calendar.toString(), calendar);
                }
                ThreadUtils.getMainHandler().post(() -> {
                    for (IAppDataCallback callback : mCallbacks) {
                        callback.onSchoolCalendarUpdate(schoolCalendarMap);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ThreadUtils.getMainHandler().post(() -> ToastUtils.showShort("校历同步失败！"));
            }
        });
    }

    @Override
    public void updatePostContent() {
        RxTool.getBackgroundHandler().post(() -> {
            try {
                AppData appData = UpdateAppDataUtils.getAppData();
                String postContentUrl = appData.getPostContent();
                String postContentJson = MyOkHttp.postData(postContentUrl, null, null);
                //LogUtils.d(this, "updatePostContent: ======> " + postContentJson);
                HttpData<String> postContentHttpData = GsonUtils.fromJson(postContentJson, new TypeToken<HttpData<String>>() {
                }.getType());
                String postContent = postContentHttpData.getData();
                //LogUtils.d(this, "updatePostContent: ======> " + postContent);
                ThreadUtils.getMainHandler().post(() -> {
                    for (IAppDataCallback callback : mCallbacks) {
                        callback.onPostContentUpdate(postContent);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ThreadUtils.getMainHandler().post(() -> ToastUtils.showShort("公告内容拉取失败！"));
            }
        });
    }

    @Override
    public void registered(IAppDataCallback callback) {
        if (!mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unRegistered(IAppDataCallback callback) {
        mCallbacks.remove(callback);
    }
}
